<?php

/**
 * AdminAction - Admin action, controlleur standard herité du framework
 *
 * @package
 * @author Alexis Eric, 
 * Erra Prod. http://www.erraprod.com
 * @copyright Copyright (c) 2012 - Erra Prod
 * @access public
 */
class AdminAction extends Erra_System_Action {

    /**
     * sendContactMessage - Envoi un message à partir du formulaire de contact
     * (appelé par Tools_ContactPopupObject sur Back_Config.phtml, Common_CalendarHeader.phtml, Common_Header.phtml
     * 
     * @param Erra_Fields_Base_Object $field
     * @return string
     */
    public static function sendContactMessage(Erra_Fields_Base_Object $field) {
        if (!strlen($field->probleme->value)) {
            $field->probleme->setError('Veuillez entrez une description de votre demande');
        }
        if (!$field->bErrors()) {
            if (filter_var($field->email->value, FILTER_VALIDATE_EMAIL)) {
                // $field->type_probleme->value = 'license' ou 'agendas'
                $targetMail = ($field->type_probleme->value == 'license') ? 'contact@mysimplyagenda.com' : 'support@mysimplyagenda.com';
                $placeHolders = array('{object}' => $field->type_probleme->value,
                    '{src}' => $field->email->value,
                    '{subject}' => $field->probleme->value);
                Erra_Utils_Tools::sendTemplatedMail($targetMail, 'Mail/Mail_Contact.phtml', $placeHolders);
                $field->probleme->setValue('');
            }
            return "FORM_CLOSE_POPUP::erraSuccess('Votre message a été correctement envoyé')";
        }
        return 'FORM_REFRESH_FIELD';
    }

    /**
     * AdminAction::login() - Logs site admin or developper in configuration panel
     *
     * @param mixed $field
     * @return
     */
    public static function login(Erra_Fields_Base_Object $field) {
        if ($field->login->value == config_get('access_control.dev_login') && $field->password->value == config_get('access_control.dev_password')) {
            Erra_System_AccessControl::setRole('dev');
            return 'RELOAD';
        } else if ($field->login->value == config_get('access_control.admin_login') && $field->password->value == config_get('access_control.admin_password')) {
            Erra_System_AccessControl::setRole('admin');
            return 'REDIRECT::default';
        } else {
            $field->setMessage(Erra_Utils_Tools::message('error', __('Bad username or password')));
        }
        return 'FORM_REFRESH_FIELD';
    }

}

?>